package salary;

import java.io.File;
import java.sql.PreparedStatement;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.apache.ibatis.session.SqlSession;




import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;

import common.ClampFactory;

//회원정보를 처리하기 위한 모든 제어 구조
public class SalaryMyba {

	static java.util.Calendar cal = java.util.Calendar.getInstance();
	int year = cal.get(cal.YEAR);  // 날짜 가져오는 소스
	
	private SqlSession session;
	SalaryVo salaryvo;

	DecimalFormat df = new DecimalFormat("###,###,###.##");

	int fileSize = 10 * 1024 * 1024;
	String fileEncoding = "utf-8";
	int listSize = 5;// 한페이지에 보여질 행수
	int blockSize = 5;// 보여질 페이지 이동 번호수
	int totSize = 0;// 검색될 데이터 총갯수
	int totPage = 0;// 전체페이지수
	int totBlock = 0;// 전체블럭
	int nowPage = 1;// 현재페이지
	int nowBlock = 1;// 현재블럭
	int startNo = 1;// 목록의 시작 위치
	int endNo = 3;// 목록의 끝 위치
	int startPage = 1;// 현재 블럭의 시작 페이지 번호
	int endPage = 2;// 현재 블럭의 끝페이지번호
	
	private ArrayList<SalaryVo> sdata;

	public void computePage() {
		totPage = (int)Math.ceil(totSize*1.0 / listSize);
		totBlock = (int)Math.ceil(totPage*1.0/blockSize);
		nowBlock = (int)Math.ceil(nowPage*1.0/blockSize);
		
		endPage = nowBlock*blockSize;
		startPage = endPage - blockSize+1;
		if(totPage < endPage) endPage = totPage;
		endNo = nowPage*listSize;
		startNo = endNo-listSize+1;
		if (totSize < endNo)
			endNo = totSize;
	}

	public SalaryMyba() {
		try {
			session = ClampFactory.getFactory().openSession();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public ArrayList<SalaryVo> list() {
		nowPage = salaryvo.getNowPage();
		//System.out.println(nowPage);
		ArrayList<SalaryVo> data = null;
		try {
			int cnt = (int) session.selectOne("salary.htotsize", salaryvo);
			totSize = cnt;
			computePage();
			salaryvo.setStartNo(startNo);
			salaryvo.setEndNo(endNo);
			data = (ArrayList<SalaryVo>) session.selectList("salary.list",
					salaryvo);
			sdata = new ArrayList<SalaryVo>();
			SalaryVo al = null;
			for(int i=0; i<data.size(); i++) {
				al = data.get(i);
				al.getSA_age();
				//System.out.println("y = " + year);
			//	System.out.println(al.getSA_age());
				//System.out.println("substring : " + Integer.parseInt((al.getSA_age().substring(0, 4))));
				
				al.setSA_age(Integer.toString(year-Integer.parseInt((al.getSA_age().substring(0, 4)))));
				//al.setSA_age(Integer.toString(year-Integer.parseInt((al.getSA_age().substring(4,al.getSA_age().length())+1))));
			sdata.add(al);
			}
			
			session.close();

		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return sdata;
	}
	
	public SalaryVo view() {
		SalaryVo data = null;
		try {
			data = (SalaryVo) session.selectOne("salary.view", salaryvo);
			data.setSA_age(Integer.toString(year-Integer.parseInt((data.getSA_age().substring(0, 4)))));
			//data.setSA_age(Integer.toString(year-Integer.parseInt(data.getSA_age())+1));
			session.close();

		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return data;
	}


	public boolean insert() {
		boolean flag = false;

		String realPath = salaryvo.getRequest().getServletContext()
				.getRealPath("WebContent");
		//System.out.println(1);
		String path = realPath.replace("\\WebContent", "");
		String pro = path.substring(path.lastIndexOf("\\") + 1, path.length());
		path = realPath.substring(0, realPath.indexOf(".")) + pro
				+ "\\WebContent\\homepage\\salary\\photo\\";
		//System.out.println(path);

		try {
			File file = new File(path);
			if (!file.exists()) {
				file.mkdir();
			}
			//System.out.println(2);
			MultipartRequest mr = new MultipartRequest(salaryvo.getRequest(),
					path, fileSize, fileEncoding, new DefaultFileRenamePolicy());
			//System.out.println(3);
			salaryvo.setSA_no(mr.getParameter("no"));
			salaryvo.setSA_pwd("1111");
			salaryvo.setSA_nm(mr.getParameter("nm"));
			salaryvo.setSA_gd(mr.getParameter("gd"));
			salaryvo.setSA_st(mr.getParameter("st"));
			salaryvo.setSA_age(mr.getParameter("age"));
			salaryvo.setSA_sx(mr.getParameter("sx"));
			salaryvo.setSA_dpm(mr.getParameter("dpm"));
			//System.out.println(4);
			salaryvo.setSA_address(mr.getParameter("address"));
			salaryvo.setSA_home(mr.getParameter("home"));
			salaryvo.setSA_car(mr.getParameter("car"));
			salaryvo.setSA_hd(mr.getParameter("hd"));
			salaryvo.setSA_email(mr.getParameter("email"));
			salaryvo.setSA_sal(mr.getParameter("sal"));
			salaryvo.setSA_marr(mr.getParameter("marr"));
			salaryvo.setSA_ph(mr.getParameter("ph"));
			salaryvo.setSA_acc(mr.getParameter("acc"));
			//System.out.println(5);
			salaryvo.setSA_bknm(mr.getParameter("bknm"));
			salaryvo.setSA_pynm(mr.getParameter("pynm"));
			salaryvo.setSA_pho(mr.getFilesystemName("pho"));
			//System.out.println(6);
			//System.out.println(salaryvo.getSA_pho());

			int cnt = session.insert("salary.insert", salaryvo);
			//System.out.println(cnt);
			if (cnt > 0) {
				flag = true;
				session.commit();
			}
			session.close();

		} catch (Exception ex) {
			ex.getStackTrace();
		}

		return flag;
	}

	
	public boolean modify() {
		boolean flag = false;

		String realPath = salaryvo.getRequest().getServletContext()
				.getRealPath("WebContent");

		String path = realPath.replace("\\WebContent", "");
		String pro = path.substring(path.lastIndexOf("\\") + 1, path.length());
		path = realPath.substring(0, realPath.indexOf(".")) + pro
				+ "\\WebContent\\homepage\\salary\\photo\\";

		try {
			File file = new File(path);
			if (!file.exists()) {
				file.mkdir();
			}
			MultipartRequest mr = new MultipartRequest(salaryvo.getRequest(),
					path, fileSize, fileEncoding, new DefaultFileRenamePolicy());
			salaryvo.setSA_nm(mr.getParameter("nm"));
			salaryvo.setSA_gd(mr.getParameter("gd"));
			salaryvo.setSA_st(mr.getParameter("st"));
			salaryvo.setSA_age(mr.getParameter("age"));
			salaryvo.setSA_sx(mr.getParameter("sx"));
			salaryvo.setSA_dpm(mr.getParameter("dpm"));
			salaryvo.setSA_address(mr.getParameter("address"));
			salaryvo.setSA_home(mr.getParameter("home"));
			salaryvo.setSA_car(mr.getParameter("car"));
			salaryvo.setSA_hd(mr.getParameter("hd"));
			salaryvo.setSA_email(mr.getParameter("email"));
			salaryvo.setSA_sal(mr.getParameter("sal"));
			salaryvo.setSA_marr(mr.getParameter("marr"));
			salaryvo.setSA_ph(mr.getParameter("ph"));
			salaryvo.setSA_acc(mr.getParameter("acc"));
			salaryvo.setSA_bknm(mr.getParameter("bknm"));
			salaryvo.setSA_pynm(mr.getParameter("pynm"));
			salaryvo.setSA_pho(mr.getFilesystemName("photo"));
			String oldfile = (mr.getParameter("photo1"));
			salaryvo.setSA_no(mr.getParameter("no"));
			/*
			 * System.out.println(salaryvo.getSA_sx()+"18");
			 */

			//System.out.println(oldfile);
			if (!(oldfile.equals(salaryvo.getSA_pho()))
					&& (salaryvo.getSA_pho() != null && !(salaryvo.getSA_pho()
							.equals("")))) {
				file = new File(path + "/" + oldfile);
				if (file.exists())
					file.delete();
			}
			//System.out.println(salaryvo.getSA_pho());


			int cnt = session.update("salary.modify", salaryvo);
			//System.out.println(cnt);
			if (cnt > 0) {
				//System.out.println(cnt + "2");
				flag = true;
				session.commit();
			} else {
				//System.out.println(cnt + "3");
				flag = false;
				session.rollback();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return flag;
	}

	
	public SqlSession getSession() {
		return session;
	}

	public void setSession(SqlSession session) {
		this.session = session;
	}

	public SalaryVo getSalaryvo() {
		return salaryvo;
	}

	public void setSalaryvo(SalaryVo salaryvo) {
		this.salaryvo = salaryvo;
	}

	public DecimalFormat getDf() {
		return df;
	}

	public void setDf(DecimalFormat df) {
		this.df = df;
	}

	public int getFileSize() {
		return fileSize;
	}

	public void setFileSize(int fileSize) {
		this.fileSize = fileSize;
	}

	public String getFileEncoding() {
		return fileEncoding;
	}

	public void setFileEncoding(String fileEncoding) {
		this.fileEncoding = fileEncoding;
	}

	public int getListSize() {
		return listSize;
	}

	public void setListSize(int listSize) {
		this.listSize = listSize;
	}

	public int getBlockSize() {
		return blockSize;
	}

	public void setBlockSize(int blockSize) {
		this.blockSize = blockSize;
	}

	public int getTotSize() {
		return totSize;
	}

	public void setTotSize(int totSize) {
		this.totSize = totSize;
	}

	public int getTotPage() {
		return totPage;
	}

	public void setTotPage(int totPage) {
		this.totPage = totPage;
	}

	public int getTotBlock() {
		return totBlock;
	}

	public void setTotBlock(int totBlock) {
		this.totBlock = totBlock;
	}

	public int getNowPage() {
		return nowPage;
	}

	public void setNowPage(int nowPage) {
		this.nowPage = nowPage;
	}

	public int getNowBlock() {
		return nowBlock;
	}

	public void setNowBlock(int nowBlock) {
		this.nowBlock = nowBlock;
	}

	public int getStartNo() {
		return startNo;
	}

	public void setStartNo(int startNo) {
		this.startNo = startNo;
	}

	public int getEndNo() {
		return endNo;
	}

	public void setEndNo(int endNo) {
		this.endNo = endNo;
	}

	public int getStartPage() {
		return startPage;
	}

	public void setStartPage(int startPage) {
		this.startPage = startPage;
	}

	public int getEndPage() {
		return endPage;
	}

	public void setEndPage(int endPage) {
		this.endPage = endPage;
	}

}
